<template>
	<div class="configBox">
		<el-form autocomplete="on" :model="configItem" ref="configItem" label-position="left" style="">
			<el-form-item style="margin-bottom: 20px;text-align: center;" v-if="!getCount">
				<el-button style="width: 45%;background-color: #aaffff;color: #000;" type="primary" :loading="loading"
					@click.native.prevent="handleGetConfig">
					获取配置</el-button>
			</el-form-item>
			<el-form-item style="margin-bottom: 20px;text-align: center;" v-else>
				<el-button style="width: 45%" type="primary" :loading="loading" @click.native.prevent="handleSetConfig">
					配置</el-button>
			</el-form-item>
			<el-form-item prop="username">
				<el-input name="username" type="text" v-model="configItem.username" autocomplete="on" placeholder="用户名"
					maxlength='20'>
				</el-input>
			</el-form-item>
			<el-form-item prop="password">
				<el-input name="password" type="password" v-model="configItem.password" autocomplete="on"
					placeholder="秘钥" show-password maxlength='16'>
				</el-input>
			</el-form-item>
			<span class="tipTile">token有效期(用户不重新登录不生效) 单位<strong>毫秒</strong> 1小时即3600000</span>
			<el-form-item prop="tokenExp">
				<el-input name="tokenExp" type="number" v-model="configItem.tokenExp" autocomplete="on"
					placeholder="token有效期" maxlength='20'>
				</el-input>
			</el-form-item>
			<span class="tipTile">面对面建群有效期 单位<strong>毫秒</strong>,30秒 即30,000</span>
			<el-form-item prop="faceTofaceExp">
				<el-input name="faceTofaceExp" type="number" v-model="configItem.faceTofaceExp" autocomplete="on"
					placeholder="面对面建群有效期" maxlength='20'>
				</el-input>
			</el-form-item>
			<span class="tipTile">面对面建群最大人数 (x>2)</span>
			<el-form-item prop="faceTofaceExp">
				<el-input name="faceTofaceExp" type="number" v-model="configItem.MaximumNumberOfPeople"
					autocomplete="on" placeholder="面对面建群最大人数" maxlength='20'>
				</el-input>
			</el-form-item>
			<span class="tipTile">面对面建群经纬度精确位数(x>=0)</span>
			<el-form-item prop="faceTofaceExp">
				<el-input name="faceTofaceExp" type="number" v-model="configItem.positionNumber" autocomplete="on"
					placeholder="面对面建群经纬度精确位数" maxlength='20'>
				</el-input>
			</el-form-item>

			<span class="tipTile">按时间清理面对面建群数据 执行速率 , 单位<strong>毫秒</strong>, 5s即5000,[非特殊情况禁止修改]</span>
			<el-form-item prop="OperationRate">
				<el-input name="OperationRate" type="number" v-model="configItem.OperationRate" autocomplete="on"
					placeholder="执行速率" maxlength='20'>
				</el-input>
			</el-form-item>
			<span class="tipTile">敏感词库[严格按照英文逗号 <strong class='color-error'>,</strong> 隔开]</span>
			<div class="shs-flex cursorPointer">
				<el-tag size='small' class="tipTile marginR15" @click="handledDict(0)">获取</el-tag>
				<el-tag type='success' size='small' class="tipTile" @click="handledDict(1)">设置</el-tag>
			</div>
			<el-form-item prop="sensitiveWords" style="margin-bottom: 80px;">
				<el-input type="textarea" name="sensitiveWords" :autosize="{ minRows: 1,maxRows:10}" resize='none'
					placeholder="敏感词库" v-model="sensitiveWords" style="height: 40px;line-height: 40px;">
				</el-input>
			</el-form-item>

		</el-form>
	</div>
</template>

<script>
	import {
		setConfig,
		getConfig
	} from "@/api/shsconfig";
	import {
		debounce
	} from "@/utils/common.js";
	import {
		getInfo,
		updateInfo
	} from "@/api/dict";

	export default {
		name: "pageConfig",
		data() {
			return {
				loading: false,
				configItem: {
					username: "shsim",
					password: "",
					tokenExp: '', //默认1小时,
					faceTofaceExp: '', //默认30秒
					OperationRate: '' //清数据 执行速率5s
				},
				getCount: 0, //获取之后才能配置
				sensitiveWords: "", //敏感词库
				code: "sensitiveWords", //数据库查询敏感词列表code
			}
		},
		mounted() {

		},
		methods: {
			handledDict: debounce(function(status) {
				if (status) {
					this.$message({
						message: "请前往数据库配置敏感词库",
						type: "warning",
						duration: 3000
					})
					return
					// updateInfo({
					// 	code: this.code,
					// 	value: this.sensitiveWords
					// }).then(res => {
					// 	this.$message({
					// 		message: "敏感词库配置成功",
					// 		type: "success",
					// 		duration: 700
					// 	})
					// }).catch(err=>{
					// 	this.$message({
					// 		message: "敏感词库配置失败"+err.message,
					// 		type: "success",
					// 		duration: 3000
					// 	})
					// })
				} else {
					this.sensitiveWords="涉及违规词库，无法展示"
					return
					// getInfo({
					// 	code: this.code
					// }).then(res => {
					// 	this.sensitiveWords = res.data[0].value
					// 	this.$message({
					// 		message: "敏感词库获取成功",
					// 		type: "success",
					// 		duration: 700
					// 	})
					// })
				}
			}),
			// 获取
			handleGetConfig: debounce(function() {
				getConfig(this.configItem).then(data => {
					this.getCount = 1;
					this.configItem = Object.assign({}, this.configItem, data);
					this.$notify({
						title: '',
						message: data.message + "-获取成功",
						type: 'success',
						duration: 1000
					})
				})
			}),
			// 配置
			handleSetConfig: debounce(function() {
				//验证
				if (!this.handledVerification()) {
					return;
				}
				setConfig(this.configItem).then(data => {
					this.getCount = 1;
					this.$notify({
						title: '',
						message: data.message,
						type: 'success',
						duration: 1000
					})
				}).catch(err => {
					this.getCount = 1;
				})
			}),
			//验证
			handledVerification() {
				if (this.configItem.faceTofaceExp <= 0 || this.configItem.positionNumber < 0 || this.configItem.tokenExp <=
					0 ||
					this.configItem.OperationRate <= 0 || this.configItem.MaximumNumberOfPeople < 2) {
					this.$notify({
						title: '',
						message: "数据非法，重新配置",
						type: 'warning',
						duration: 1000
					})
					return false;
				}

				return true;
			}
		}
	}
</script>

<style scoped>
	.tipTile {
		display: block;
		font-size: 14px;
		color: #ccc;
	}

	.configBox {
		width: 90%;
		margin-left: 5%;
		margin-top: 5%;
		margin-bottom: 100px;
		padding-bottom: 100px;
	}
</style>